﻿//普通字符域的判断
function sl_check(field, name, maxlength, minlength)
{
	var sMsg="";
	if(minlength != 0 && field.value == "")
	{
		sMsg=name+"\t不能为空，请您填写"+name+"！\n\n";
		field.focus();
		return sMsg;
	}

	if(minlength != 0 && ansiLength(field.value) < minlength)
	{
		if(minlength == maxlength)
			sMsg+=name+"\t的长度必须是"+minlength+"个字节，请您重新填写！\n\n";
		else
			sMsg+=name+"\t的长度至少需要"+minlength+"个字节以上，请您重新填写！\n\n";
		field.focus();
		return sMsg;
	}
	if(maxlength != 0 && ansiLength(field.value) > maxlength)
	{
		sMsg+=name+"\t的长度不能超过"+maxlength+"个字节，请您重新填写！\n\n";
		field.focus();
		return sMsg;
	}
	return sMsg;
}

//非负整数
function sl_checkInteger(field, name, maxlength, minlength)
{
	var sMsg=sl_check(field, name, maxlength, minlength);

	if(sMsg!=""){
		return sMsg;
	}

	if(field.value==""){
		return "";
	}

	 if(!isInteger(field.value)){
			sMsg+=name+"\t必须是整数数字，请您重新填写！\n\n";
			field.focus();
			return sMsg;
	}


	return sMsg;
}

//整数
function sl_checkInt(field, name, maxlength, minlength)
{
	var sMsg=sl_check(field, name, maxlength, minlength);

	if(sMsg!=""){
		return sMsg;
	}

	if(field.value==""){
		return "";
	}

	if(!isInteger(field.value)){
			sMsg+=name+"\t必须是整数数字，请您重新填写！\n\n";
			field.focus();
			return sMsg;
	}


	return sMsg;
}

//小数
function sl_checkFloat(field, name, intLength,floatLength, minlength)
{
	var sMsg=sl_check(field, name, intLength+floatLength+1, minlength);

	if(sMsg!=""){
		return sMsg;
	}
	
	var str = field.value;
	
	if(str==""){
		return "";
	}
	
	if(!isFloat(str)){
			sMsg+=name+"\t必须是数字，请您重新填写！\n\n";
			field.focus();
			return sMsg;
	}
	
	var pos = str.indexOf(".");
	if(pos!=-1){
		if(str.length-pos>floatLength+1){
			sMsg += name+"\t小数部分不能超过"+floatLength+"位，请调整\n\n";
			return sMsg;
		}
	}
	return sMsg;
}

//邮件
function sl_checkEmail(field, name, maxlength, minlength)
{
	var sMsg=sl_check_remark(field, name, maxlength, minlength);
	if(sMsg!=""){
		return sMsg;
	}
	
	var s=trim(field.value);
	if(!isEmail(s)){
			sMsg+=name+"不是有效邮件地址！\n\n";
			field.focus();
			return sMsg;
	}

	return sMsg;
}

//电话
function sl_checkPhone(field, name, maxlength, minlength){
	var sMsg=sl_check(field, name, maxlength, minlength);
	if(sMsg!=""){
		return sMsg;
	}
	
	var s=trim(field.value);
	if(!isPhone(s)){
			sMsg+=name+"不是有效电话号码！\n\n";
			field.focus();
			return sMsg;
	}
	return sMsg;
}

//手机
function sl_checkMobile(field, name, maxlength, minlength){
	var sMsg=sl_check(field, name, maxlength, minlength);
	if(sMsg!=""){
		return sMsg;
	}
	
	var s=trim(field.value);
	if(!isMobile(s)){
			sMsg+=name+"不是有效手机号码！\n\n";
			field.focus();
			return sMsg;
	}
	return sMsg;
}

//日期
function sl_checkDate(field, name){
	var sMsg=sl_check(field, name, 10,0);
	if(sMsg!=""){
		return sMsg;
	}
	
	var str=trim(field.value);
	if(!isDate(str)){
			sMsg+=name+"必须为日期格式yyyy-mm-dd！\n\n";
			field.focus();
			return sMsg;
	}
	return sMsg;
}

function sl_checkChoice(field, name)
{
	var sMsg="";
	if (getRadioValue(field)==null)
	{
		sMsg="请选择"+name+"！\n\n";
	}
	return sMsg;
}

function sl_checkOption(field, name)
{
	var sMsg="";
	if (field.value=="")
	{
		sMsg="请选择"+name+"！\n\n";
	}
	return sMsg;
}

function sl_alert(errinfo, url)
{
	alert("系统提示：　　　　\n\n" + errinfo + "\n\n");
	if (url != null)
		location.replace(url);
}


function sl_confirm(confirm_info, vbVersion)
{
	if (vbVersion == null){
				return confirm("系统确认：\n\n您确认要" + confirm_info + "吗？");
		}
	else
		return sl_vb_comfirm("系统确认：\n\n您确认要" + confirm_info + "吗？");
}

function sl_check_update(doNew)
{
	if (doNew == "null")
		return sl_confirm("保存");
	else
		return sl_confirm("保存修改");
}

function confirmRemove(element)
{
	if (element == null)
	{
		sl_alert("未选定任何记录！");
		return false;
	}
	if(checkedCount(element) == 0)
	{
		sl_alert("请选定要删除的记录！");
		return false;
	}
	return sl_confirm('删除选定的记录');
}

/*
function ansiLength(str)
{
	return str.length;
}
*/
function ansiLength(str){
	var len=0;
	if(str.length>0){
		for (var i=0; i<str.length; i++) {
	        var charCode = str.charCodeAt(i);
	        if (charCode <= 0x0000007f){
	            len += 1;
	        } else if (charCode >= 0x00000080 && charCode <= 0x000007FF) {
	            len += 2;
	        } else if (charCode >= 0x00000800 && charCode <= 0x0000FFFF) {
	            len += 3;
	        } else if (charCode >= 0x00010000 && charCode <= 0x001FFFFF) {
	            len += 4;
	        } else if (charCode >= 0x00200000 && charCode <= 0x03FFFFFF) {
	            len += 5;
	        } else if (charCode >= 0x04000000  && charCode <= 0x7FFFFFFF) {
	            len += 6;
	        }
	    }
	}
	return len;
}


function checkedCount(element)
{
	var iCount = 0;
	var i;

	if(element == null)
		return 0;

	if(element.length == null)
	{
		if(element.checked)
			return 1;
		else
			return 0;
	}

	for(i = 0; i < element.length; i++){
		if(element[i].checked){
			iCount++;
		}
	}

	return iCount;
}

function selectAll(element)
{
	if(element == null){
		return;
	}

	var iCount = checkedCount(element);
	var i;

	if(element.length == null){
		element.checked = iCount < 1;
		return;
	}

	for(i = 0; i < element.length; i++){
		element[i].checked = iCount < element.length;
	}
}

function setCheckboxValue(element,selvalue){
	if(element == null)
		return;

	if(element.length == null)
	{
		return;
	}
	selvalue=","+selvalue+",";
	for(var i = 0; i < element.length; i++){
		if(selvalue.indexOf(","+element[i].value+",")>=0){
			element[i].checked=true;
		}
	}
}

function setRadioValue(field,selvalue){
	if(field == null){
		return;
	}

	if(field.length == null){
		return;
	}
	
	for(var i=0;i<field.length;i++){
        if(field[i].value==selvalue){
            field[i].checked=true;
            break;
        }
    }
}

function checkRadioStat(elementName) {
	var element = document.getElementsByName(elementName);
	var flag = false;
	for (var i = 0; i < element.length; i++) {
		if (element[i].checked == true) {
			flag = true;
		}
	}
	return flag;
}

function getRadioValue(field){
	var v = null;
    for(var i=0;i<field.length;i++){
        if(field[i].checked==true){
            v = field[i].value;
        }
    }
    return v;
}


function isNull(str){
	if(trim(str)==""){
		return true;
	}else{
		return false;
	}
	
}

//trim str.
function trim(str)
{
	var strTmp;
	if ((str=="")||(str==null))
		return "";
	for(var i = 0 ; i<str.length && str.charAt(i)==" " ; i++ ) ;
	strTmp = str.substring(i,str.length);

	for(var i = strTmp.length - 1; i > 0 && strTmp.charAt(i)==" "; i--) ;
	strTmp = strTmp.substring(0,i+1);

	return strTmp;
}



function getDateDif(curdate,difdate){
	var dt=new Date(curdate.getFullYear(),curdate.getMonth(),curdate.getDate()+difdate);
	return new Date(dt.getTime());
}

function getDateStr(dt){
	return dt.getFullYear()+"-"+(dt.getMonth()+1)+"-"+dt.getDate();
}


//如日期 ："yyyy-mm-dd"
//如果前面的日期大于后面的日期则返回结果>0, 否则<0
function compareDate(Date1, Date2) {
	return compareDate1(Date1.replace(/-/g,"/"),Date2.replace(/-/g,"/"));
}

//比较日期："yyyy/mm/dd"
function compareDate1(sTime1,sTime2) {
	t1 = Date.parse(sTime1);
	t2 = Date.parse(sTime2);
	return t1 - t2;
}

function getTimeStr()
{
	var dt=new Date();
	return "ts="+dt.getTime();
}

/*对小数部分进行四舍五入*/
function getfloatAtnum(number,floatlength){
	 var newnumber;

	 number = ''+number;
	 floatlength =''+floatlength;

		var oldnumber=parseFloat(number)*Math.pow(10,floatlength);
		newnumber=Math.round(oldnumber)/Math.pow(10,floatlength);

		return newnumber;
}

//设置数字定长。不够前补0
function ConvNumAsLen(num,len){
	var str=trim(""+num);
	if(str.length>=len){
		return str;
	}
	s="";
	for(var i=0;i<len-str.length;i++){
		s=s+"0";
	}
	return s+str;
} 



function isInt(s){
	if (s.match("^-?\\d+$")){
		return true;
	}
	else{
		return false;
	}
}

function isFloat(s){
	if (s.match("^(-?\\d+)(\\.\\d+)?$")){
		return true;
	}
	else{
		return false;
	}
}

function isInteger(s){
	if (s.match("^\\d+$")){
		return true;
	}
	else{
		return false;
	}
}

function isEmail(s){
	if (s.match("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.([a-zA-Z0-9_-])+)+$")){
		return true;
	}
	else{
		return false;
	}
}

function isMobile(s){
	if (s.match("^1[0-9]{2}\d{8}$")){
		return true;
	}
	else{
		return false;
	}
}

function isPhone(s){
	if (s.match("^([0-9]|[\-])+$")){
		return true;
	}
	else{
		return false;
	}
}

function isDate(s){
	if (s.match("^[\d]{4}\-[\d]{2}\-[\d]{2}$")){
		return true;
	}
	else{
		return false;
	}
}

/**
 * url:/SimpleDataxWeb/renderSndData
 * param:{"userName":form.userName.value,"password":form.password.value}
 */
function getDataByAjax(url,param){
	var result = null;
	
	$.ajax({
	      type:"POST",
	      async:false,
	      cache:false,
	      url:url,
	      dataType:"json",
	      data:param,
	      success:function(d){
	    	  result = d;
	      },
	      error:jqueryAjaxError
	});
	
	return result;
}

function jqueryAjaxError(XMLHttpRequest,textStatus, errorThrown){
	if(XMLHttpRequest.status == 200){
		var responseText = trim(XMLHttpRequest.responseText);
		if(responseText!=""){
			var errJson = eval('(' + trim(responseText) +')');
			alert(errJson.serviceErrorMessage);
			//alert(errJson.serviceException);
		}
	}else{
		alert(XMLHttpRequest.status+":"+XMLHttpRequest.statusText);
	}
}
